package com.itheima.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.common.R;
import com.itheima.common.pageParm;
import com.itheima.dto.DishDto;
import com.itheima.service.dishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/dish")
@RestController
public class DishController {


    @Autowired
    private dishService dishService;

    @PostMapping
    public R add(@RequestBody DishDto dishDto) {

        int row = dishService.add(dishDto);

        if (row > 0) {
            return R.success("添加菜品成功！");
        } else {
            return R.error("添加菜品失败！");
        }
    }

    @GetMapping("/page")
    public R findPage(pageParm pageParm) {

        IPage<DishDto> iPage = dishService.findPage(pageParm);

        return R.success(iPage);
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        int row = dishService.update(dishDto);

        if (row > 0) {
            return R.success("更新成功！");
        } else {
            return R.error("更新失败！");
        }
    }

    @GetMapping("/list")
    public R findByCategoryId(long categoryId, Integer status) {

        List<DishDto> list = dishService.findByCategoryId(categoryId, status);

        return R.success(list);
    }

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){

       int row= dishService.delete(ids);


       if(row>0){
           return R.success("删除成功！");
       }else{
           return R.error("删除失败！");
       }
    }

    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable int status, @RequestParam List<Long> ids) {
        int row = dishService.updateStatusByIds(ids, status);
        if(row > 0) {
            return R.success(status == 1 ? "菜品启售成功" : "菜品停售成功");
        }
        return R.error(status == 1 ? "菜品启售失败" : "菜品停售失败");
    }
}
